package main;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import messageHandler.GlobalMsgHandler;
import model.IAction;
import model.PlayerInfo;
import network.Session;
import utilities.GameResource;

public class NuoiThuMidlet extends MIDlet {
	public NuoiThuMidlet() {
		// TODO Auto-generated constructor stub
        Session.gI().setHandler(GlobalMsgHandler.gI());
        instance = this;
        gameCanvas = new GameCanvas();
        gameCanvas.start();
        GameCanvas.instance.initGameCanvas();
        //display login screen
        GameCanvas.loginScr.switchToMe();
    }
	
	public static String IP = "localhost";
	//public static String IP = "210.211.97.6";
	public static byte PROVIDER_ID = 25;
	
    public static int PORT = 30121;
	public static String version = "1.0.0";		//current version
	    
	public static GameCanvas gameCanvas;
	public static NuoiThuMidlet instance;
	public static PlayerInfo m_myPlayerInfo;

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        Display.getDisplay(this).setCurrent(gameCanvas);
    }

    //send sms
    public static void sendSMS(final String data, final String to, final IAction successAction, final IAction failAction) {
        final String address = to;
        new Thread(new Runnable() {
            public void run() {
                try {
                    MessageConnection smsconn = (MessageConnection) Connector.open(GameResource.smsPrefix + address);
                    TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
                    txtmessage.setAddress(GameResource.smsPrefix + address);
                    txtmessage.setPayloadText(data);
                    smsconn.send(txtmessage);
                    successAction.perform();
                } catch (Exception e) {
                    failAction.perform();
                }
            }
        }).start();
    }

    public void sendSMS(String strData, String strSmsTo) {
        // TODO Auto-generated method stub
        //send to server
    	NuoiThuMidlet.sendSMS(strData, strSmsTo, new IAction() {

            public void perform() {
                GameCanvas.startOKDlg(GameResource.smsSent);
            }
        }, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                GameCanvas.startOKDlg(GameResource.sendSMSFail);
            }
        });
    }
}
